package cn.itheima.web;

import cn.itheima.domain.User;
import cn.itheima.domain.UserQueryVo;
import cn.itheima.service.UserService;
import cn.itheima.service.UserServiceImp;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author: cuisensen
 * data: 2018/7/4.
 * time: 16:56.
 */
@WebServlet("/findUsers")
public class FindUsersServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserQueryVo query=new UserQueryVo();
        try {
            BeanUtils.populate(query,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        UserService us=new UserServiceImp();
        List<User> users = us.findUserByTerm(query);
        System.out.println(query);
        for (User u : users){
            System.out.println(u);
        }
        request.setAttribute("users",users);
        request.getRequestDispatcher(request.getContextPath()+"/index.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
